<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="工作标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入工作标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="公司名称" prop="companyName">
        <el-input
          v-model="queryParams.companyName"
          placeholder="请输入公司名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="标签" prop="workTags">
        <el-input
          v-model="queryParams.workTags"
          placeholder="请输入标签"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['works:sideWork:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['works:sideWork:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['works:sideWork:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['works:sideWork:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="sideWorkList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="工作标题" width="300px" show-overflow-tooltip align="center" prop="title"/>
      <!--<el-table-column label="工作简介" align="center" prop="workContent"/>-->
      <el-table-column label="单价" width="120px" align="center" prop="workPrice"/>
      <el-table-column label="单位" width="120px" align="center" prop="priceUnit">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.work_price_unit" :value="scope.row.priceUnit"/>
        </template>
      </el-table-column>
      <el-table-column label="公司名称" width="180px" show-overflow-tooltip align="center" prop="companyName"/>
      <el-table-column label="标签" align="center" width="180px" show-overflow-tooltip prop="workTags"/>
      <el-table-column label="需求数量" align="center" width="120px" prop="needCount"/>
      <el-table-column label="工作省" align="center" width="120px" prop="workProvinceName"/>
      <el-table-column label="工作市" align="center" width="120px" prop="workCityName"/>
      <el-table-column label="工作区" align="center" width="120px" prop="workAreaName"/>
      <el-table-column label="详细地址" align="center" width="180px" show-overflow-tooltip prop="workAddress"/>
      <el-table-column label="开始时间" align="center" prop="workTimeStart" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.workTimeStart, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="workTimeEnd" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.workTimeEnd, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="年龄区间" align="center" width="160px" prop="workAge"/>
      <el-table-column label="工作热度" align="center" width="120px" prop="workHot"/>
      <el-table-column label="操作" align="center"  width="160" fixed="right" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['works:sideWork:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['works:sideWork:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改兼职管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="70%" :close-on-click-modal="false" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="工作标题" prop="title">
              <el-input v-model="form.title" placeholder="请输入工作标题"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="类型" prop="workType">
              <el-select v-model="form.workType" placeholder="请选择类型">
                <el-option
                  v-for="dict in workTypeList"
                  :key="dict.id"
                  :label="dict.typeName"
                  :value="dict.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="公司" prop="companyName">
              <el-input @focus="handleOpenSelectCompany" readonly v-model="form.companyName" placeholder="请选择公司"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="工作简介">
              <editor v-model="form.workContent" :min-height="192"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="单价" prop="workPrice">
              <el-input v-model="form.workPrice" placeholder="请输入单价"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="单位" prop="priceUnit">
              <el-select v-model="form.priceUnit" @change="priceUnitChange" placeholder="请选择单位">
                <el-option
                  v-for="dict in dict.type.work_price_unit"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="需求数量" prop="needCount">
              <el-input v-model="form.needCount" placeholder="请输入岗位需求数量"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="工作省份" prop="workProvince">
              <el-select v-model="form.workProvince" @change="e => handleProvinceChange(e, true)" placeholder="请选择工作地省份">
                <el-option
                  v-for="dict in provinceList"
                  :key="dict.id"
                  :label="dict.name"
                  :value="dict.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="工作市" prop="workCity">
              <el-select v-model="form.workCity" @change="e => handleCityChange(e, true)" placeholder="请选择工作地市">
                <el-option
                  v-for="dict in cityList"
                  :key="dict.id"
                  :label="dict.name"
                  :value="dict.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="区/县" prop="workArea">
              <el-select v-model="form.workArea" @change="handleAreaChange" placeholder="请选择工作地市">
                <el-option
                  v-for="dict in areaList"
                  :key="dict.id"
                  :label="dict.name"
                  :value="dict.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="详细地址" prop="workAddress">
              <el-input v-model="form.workAddress" placeholder="请输入工作地详细地址"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="标签" prop="workTags">
              <el-input v-model="form.workTags" placeholder="请输入标签"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="开始时间" prop="workTimeStart">
              <el-date-picker clearable
                              v-model="form.workTimeStart"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择工作开始时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="结束时间" prop="workTimeEnd">
              <el-date-picker clearable
                              v-model="form.workTimeEnd"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择工作结束时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="年龄区间" prop="workAge">
              <el-input v-model="form.workAge" placeholder="请输入工作年龄区间"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 选择客户 -->
    <customer-select v-if="openCompany" :open="openCompany" @close="handleCloseCompany" @submit="handleSelectedCompany" :limit="1" />
  </div>
</template>

<script>
import CustomerSelect from "@/components/CustomerSelect/index";
import {listSideWork, getSideWork, delSideWork, addSideWork, updateSideWork} from "@/api/works/sideWork";
import { listSideWorkTypeAll } from "@/api/works/sideWorkType";
import { listSysAddressLibrary } from "@/api/system/sysAddressLibrary";


export default {
  name: "SideWork",
  components: {CustomerSelect},
  dicts: ['work_price_unit'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 兼职管理表格数据
      sideWorkList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        workContent: null,
        workPrice: null,
        priceUnit: null,
        companyId: null,
        companyName: null,
        workTags: null,
        needCount: null,
        workProvince: null,
        workProvinceName: null,
        workCity: null,
        workCityName: null,
        workArea: null,
        workAreaName: null,
        workAddress: null,
        workTimeStart: null,
        workTimeEnd: null,
        workAge: null,
        workHot: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      openCompany: false,
      workTypeList: [],
      cityList: [], // 市列表
      areaList: [], // 区列表
    };
  },
  computed: {
    provinceList() {
      const addressList = this.$store.getters.addressList;
      return addressList.filter(x => x.parentId == 0);
    }
  },
  created() {
    this.getList();
    this.loadTypeAll();
  },
  methods: {
    /** 查询兼职管理列表 */
    getList() {
      this.loading = true;
      listSideWork(this.queryParams).then(response => {
        this.sideWorkList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        workContent: null,
        workPrice: null,
        priceUnit: null,
        priceUnitName: null,
        companyId: null,
        companyName: null,
        workTags: null,
        needCount: null,
        workProvince: null,
        workProvinceName: null,
        workCity: null,
        workCityName: null,
        workArea: null,
        workAreaName: null,
        workAddress: null,
        workTimeStart: null,
        workTimeEnd: null,
        workAge: null,
        workHot: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "创建兼职信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSideWork(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改兼职信息";
        if (this.form.workProvince) {
          this.handleProvinceChange(this.form.workProvince,false);
        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSideWork(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSideWork(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除兼职管理编号为"' + ids + '"的数据项？').then(function () {
        return delSideWork(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('works/sideWork/export', {
        ...this.queryParams
      }, `sideWork_${new Date().getTime()}.xlsx`)
    },
    /** 打开公司选择 */
    handleOpenSelectCompany() {
      this.openCompany = true;
    },
    /** 关闭公司选择 */
    handleCloseCompany() {
      this.openCompany = false;
    },
    /** 确认选择 */
    handleSelectedCompany(rows) {
      if (rows && rows.length > 0) {
        this.form.companyId = rows[0].id;
        this.form.companyName = rows[0].customerName;
      } else {
        this.form.companyId = '';
        this.form.companyName = '';
      }
    },
    loadTypeAll() {
      listSideWorkTypeAll().then(res => {
        if (res.code === 200) {
          this.workTypeList = res.data || [];
        }
      });
    },
    /** 省份变化触发 */
    handleProvinceChange(target, clear) {
      const allAddress = this.$store.getters.addressList;
      const provinceArr = allAddress.filter(x => x.id == target);
      this.form.workProvinceName = provinceArr[0].name;
      // 获取市信息
      listSysAddressLibrary({parentId: target}).then(res => {
        if (res.code === 200) {
          this.cityList = res.data;
        }
      });
      // 初始化市区县
      if (clear) {
        this.form.workCity = '';
        this.form.workCityName = '';
        this.form.workArea = '';
        this.form.workAreaName = '';
        this.areaList = [];
      } else {
        if (this.form.workCity) {
          this.handleCityChange(this.form.workCity, false);
        }
      }
    },
    /** 市变化触发 */
    handleCityChange(target, clear) {
      const cityArr = this.cityList.filter(x => x.id == target);
      this.form.workCityName = cityArr[0].name;
      // 获取区信息，进行过滤
      listSysAddressLibrary({parentId: target}).then(res => {
        if (res.code === 200) {
          this.areaList = res.data;
        }
      });
      // 初始化区县
      if (clear) {
        this.form.workArea = '';
        this.form.workAreaName = '';
      }
    },
    /** 区变化触发 */
    handleAreaChange(target) {
      const areaArr = this.areaList.filter(x => x.id == target);
      this.form.workAreaName = areaArr[0].name;
    },
    /** 价格单位变化触发 */
    priceUnitChange(target) {
      const dict = this.dict.type.work_price_unit.filter(x => x.value == target);
      if (dict && dict.length > 0) {
        this.form.priceUnitName = dict[0].label;
      } else {
        this.form.priceUnitName = null;
      }
    }
  }
};
</script>
